package com.shenmazong.serveruserapi.controller;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.util.UUID;

/**
 * @author 田不平
 * @project server-xing-00
 * @date 2020/8/14 5:22 PM
 * http://www.shenmazong.com
 */

@Controller
@Slf4j
@ApiIgnore
public class QiNiuOssController {

    @GetMapping(value = "/qiniu")
    public String qiniu() {
        return "qiniu";
    }

    String qiniuUpload(MultipartFile file, String fileName) {
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.huadong());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);

        //...生成上传凭证，然后准备上传
        String accessKey = "aHsqlIGIgD4DftMtashrb1AXyiAUgk9VSTsmyiSh";
        String secretKey = "UJRMnTOX0DwLbs35vdh9Q1O3N2lyl8om_hqR9do0";
        String bucket = "shenmazong";

        //如果是Windows情况下，格式是 D:\\qiniu\\test.png
        //String localFilePath = "/Users/hm/david/pics/01.jpeg";
        //默认不指定key的情况下，以文件内容的hash值作为文件名

        String key = fileName;
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(file.getInputStream(), key, upToken, null, null);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            log.info(putRet.key);
            log.info(putRet.hash);
            log.info("fileName:"+fileName);

            // 成功返回
            String url = "http://q6stzzqhz.bkt.clouddn.com/" + putRet.key;
            return url;

            // http://q6stzzqhz.bkt.clouddn.com/Flk9PUdOw-TtSg0PUZebPo0ukL9b
            // http://q6stzzqhz.bkt.clouddn.com/Flk9PUdOw-TtSg0PUZebPo0ukL9b

        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @PostMapping(value = "/qiniuupload")
    public String upload(@RequestParam("uploadFile") MultipartFile file) {
        if(file.isEmpty()){
            log.info("文件为空");
            return "qiniu";
        }
        String fileName = file.getOriginalFilename();
        int size = (int) file.getSize();
        log.info(fileName + "-->" + size);

        UUID uuid = UUID.randomUUID();
        String suffix = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
        String newName = uuid.toString() + suffix;

        String url = qiniuUpload(file, newName);
        if(url == null) {
            log.info("上传失败了");
            return "qiniu";
        }
        log.info("上传成功: " + url);

        return "qiniu";
    }
}
